package com.cdie.myschool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cdie.myschool.common.R;
import com.cdie.myschool.common.enums.ResultCodeEnum;
import com.cdie.myschool.domain.dto.ScoreDTO;
import com.cdie.myschool.domain.entity.Score;
import com.cdie.myschool.domain.vo.ScoreVO;
import com.cdie.myschool.mapper.StudentScoreMapper.StudentScoreMapper;
import com.cdie.myschool.service.StudentScoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class StudentScoreServiceImpl implements StudentScoreService {

    @Autowired
    private StudentScoreMapper studentScoreMapper;
    /**
     * 本学期学分总数
     * @param scoreDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class) //全局异常统一处理
    public Integer showScore(ScoreDTO scoreDTO) {
        LambdaQueryWrapper<Score> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //SELECT sum(obtaining_credits) FROM score WHERE student_id="1890204000" and semester_id=26;
        List<Score> scoreList = studentScoreMapper.selectList(lambdaQueryWrapper
                .eq(Score::getStudentId,scoreDTO.getStudentId()).eq(Score::getSemesterId,scoreDTO.getSemesterId()));
        Integer sumscore = 0;
        for (Score score:scoreList
             ) {
           sumscore += score.getObtainingCredits();
        }
//        ScoreVO scoreVO = new ScoreVO();
//        scoreVO.setObtainingCredits(sumscore);
        System.out.println("学分总分为：" + sumscore);
        return sumscore;
    }
}
